package com.bite.spring.demo.controller;

import com.bite.spring.demo.model.MessageInto;
import com.bite.spring.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

//关于留言板的两个接口的实现；
@RequestMapping("/message")
@RestController
public class MessageController {
    //进行向该类中进行注入业务逻辑实现类的实例对象；
    @Autowired
    private MessageService messageService;
//    private List<MessageInto> messageIntoList=new ArrayList<>();
    @RequestMapping("/publish")
   // @RequestMapping(value = "/publish",produces = "application/json")
    public String publish(@RequestBody MessageInto messageInto){
        if(!StringUtils.hasLength(messageInto.getFrom())
                ||!StringUtils.hasLength(messageInto.getTo())
        ||!StringUtils.hasLength(messageInto.getMessage())){
            return "{\"ok\":0}";
        }
        messageService.addMessage(messageInto);
        return "{\"ok\":1}";
    }
    @RequestMapping("/getList")
    public List<MessageInto> getList(){
        //进行从业务逻辑层（即：Service)中进行获取所有的用户留言，获取到后交给前端进行展示；
        return messageService.queryAll();
    }
}
